package com.example.h2o.sqlitedemo3.sql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 *
 * @author h2o
 */
public class MyDatabase {
    Context mContext;
    MyHelper myHelper;
    SQLiteDatabase sqLiteDatabase;

    public MyDatabase(Context context){
        mContext = context;
        myHelper  = new MyHelper(mContext);
        sqLiteDatabase = myHelper.getReadableDatabase();
    }

    /**
     * 增
     */
    public void insert(){
        /*MyHelper myHelper  = new MyHelper(mContext
        SQLiteDatabase sqLiteDatabase = myHelper.getReadableDatabase();*/
        ContentValues values = new ContentValues();
        values.put("name","345");
        values.put("sex","男");
        //values.put("id","2");
        sqLiteDatabase.insert("teacher",null,values);

    }
    //删
    public void delete(){
        /*MyHelper myHelper  = new MyHelper(mContext);
        SQLiteDatabase sqLiteDatabase = myHelper.getReadableDatabase();*/
    /*    ContentValues values = new ContentValues();
        values.put("name","123");*/
        //values.put("id","2");
        sqLiteDatabase.delete("teacher","id= ?",new String []{"1"});

    }
    //改
    public int update(String name,String price){
        ContentValues values = new ContentValues();
        values.put("price",price);
        int number = sqLiteDatabase.update("information",values,"name=?",new String[]{name});
        return  number;
    }
    //查
    public boolean select(int id){
        Cursor cursor = sqLiteDatabase.query("information",null,"_id=?",new String[]{id+""},null,null,null);
        boolean result = cursor.moveToNext();
        return result;
    }

    public void dbClose(){
        sqLiteDatabase.close();
        myHelper.close();
    }
}
